home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / overview / dtscpluslibrary / sources / soundclass.cp < prev    next >
Encoding:
Text File  |  2000-06-23  |  2.5 KB  |  87 lines

  1. /*
  2.     File:        SoundClass.cp
  3.  
  4.     Contains:    TSound is a simple object that plays sounds     
  5.                   TSound.cp contains the TSound member functions. 
  6.  
  7.     Written by: Kent Sandvik    
  8.  
  9.     Copyright:    Copyright © 1992-1999 by Apple Computer, Inc., All Rights Reserved.
  10.  
  11.                 You may incorporate this Apple sample source code into your program(s) without
  12.                 restriction. This Apple sample source code has been provided "AS IS" and the
  13.                 responsibility for its operation is yours. You are not permitted to redistribute
  14.                 this Apple sample source code as "Apple sample source code" after having made
  15.                 changes. If you're going to re-distribute the source, we require that you make
  16.                 it clear in the source that the code was descended from Apple sample source
  17.                 code, but that you've made changes.
  18.  
  19.     Change History (most recent first):
  20.                 8/18/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  21.                 
  22.  
  23. */
  24. #ifndef _SOUND_
  25. #include "SoundClass.h"
  26. #endif
  27.  
  28.  
  29. // CONSTRUCTORS AND DESTRUCTORS
  30. #pragma segment Sound
  31. TSound::TSound(char* name)
  32. // Construct the TSound class using the string name for the sound resource.
  33. {
  34.     strcpy(fSound, name);
  35.     c2p(fSound);                                // convert it to a Pascal string
  36.  
  37.     fSoundHandle = ::GetNamedResource(kSoundType, (ConstStr255Param)fSound);// get the resource
  38.     ASSERT(fSoundHandle != NULL, "\pDidn't find the Sound Resource");
  39.     if (fSoundHandle != NULL)
  40.         ::DetachResource(fSoundHandle);            // detach it so we could have multiple copies in memory
  41. }
  42.  
  43.  
  44. TSound::TSound(short resID)
  45. // Construct the TSound class using a resource ID.
  46. {
  47.     fSoundID = resID;
  48.     fSoundHandle = ::GetResource(kSoundType, fSoundID);
  49.     ASSERT(fSoundHandle != NULL, "\pDidn't find the Sound Resource");
  50.     if (fSoundHandle != NULL)
  51.         ::DetachResource(fSoundHandle);            // detach it so we could have multiple copies in memory
  52. }
  53.  
  54.  
  55. #pragma segment Sound
  56. TSound::~TSound()
  57. // Destruct the sound handle.
  58. {
  59.     if (fSoundHandle != NULL)                    // we have a valid snd handle
  60.     {
  61.         ::DisposeHandle(fSoundHandle);
  62.     }
  63. }
  64.  
  65.  
  66. // MAIN INTERFACE
  67. #pragma segment Sound
  68. void TSound::Play()
  69. // Play the selected sound.
  70. {
  71.     if (fSoundHandle != NULL)                    // we have a valid snd handle
  72.     {
  73.         fError = ::SndPlay(NULL, (SndListHandle)fSoundHandle, false);// play synchronously
  74.         VASSERT(fError == noErr, ("Problems with SndPlay = %d\n", fError));
  75.     }
  76. }
  77.  
  78.  
  79. // _________________________________________________________________________________________________________ //
  80.  
  81.  
  82. /*    Change History (most recent last):
  83.   No        Init.    Date        Comment
  84.   1            khs        12/21/92    New file
  85.   2            khs        1/14/93        Cleanup
  86. */
  87.